<?php

namespace app\common\behavior;

use app\common\model\ToolModel;

/**
 * 模块初始化 行为
 * @author xiejunyu
 */
class Init
{
    public static $permission=[];//操作权限
    public static $shopPermission=[];//店铺数据权限
    public static $countryPermission=[];//国家数据权限
    public static $user=[];//用户信息
    public function run(&$params)
    {
        //文件目录
        defined('FILE_PATH') or define('FILE_PATH', ROOT_PATH . 'public' . DS . 'attachment' . DS);
        //当前时间
        defined('TIME') or define('TIME', date('Y-m-d H:i:s'));
        $user = session('user');
        $request = \think\Request::instance();
        $module=$request->module();
        $url = $module . '/' . $request->controller() . '/' . $request->action();
        $url=strtolower($url);
        //true 需要登陆 false不需要登陆
        $isLogin=in_array($module,['tasks','api'])?false:true;
        if (!empty($user) && !empty($user['id'])) {
            //$user=ToolModel::findByAttributes('user',['id'=>$user['id']]);
            Init::$user=$user;
            defined('UID') or define('UID', $user['id']);
            defined('CN_NAME') or define('CN_NAME', $user['cn_name']);
        } else {
            defined('UID') or define('UID', 0);
            defined('CN_NAME') or define('CN_NAME', 0);
            if($isLogin){
                if ($url != 'index/user/login') {
                    returnJson(1001, '登录失效请重新登陆!');
                }
            }
        }
        $this->log($request);
        if($isLogin){
            $this->ack($user, $url);
        }
    }

    //记录访问日志
    public function log($request)
    {
        $param = $request->param();
        $log = [];
        $log['user'] = CN_NAME;
        $log['module'] = $request->module();
        $log['controller'] = $request->controller();
        $log['action'] = $request->action();
        $log['ip'] = $request->ip();
        $log['ctime'] = TIME;
        $notAction = ['33', '22'];//不记录data的操作,避免记录数据大
        if (in_array($log['controller'] . $log['action'], $notAction)) {
            $log['data'] = '';
        } else {
            $log['data'] = json_encode($param, JSON_UNESCAPED_UNICODE);
        }
        db('access_log')->insert($log);
    }

    //检验操作权限
    public function ack($user, $url)
    {
        $permission = require_once(APP_PATH . 'permission.php');
        $pn = [];
        foreach ($permission as $v) {
            if (!empty($v['url'])) {
                $pn[$v['id']] = strtolower($v['url']);
            }
            if (!empty($v['childs'])) {
                foreach ($v['childs'] as $v2) {
                    if (!empty($v2['url'])) {
                        $pn[$v2['id']] = strtolower($v2['url']);
                    }
                    if (!empty($v2['childs'])) {
                        foreach ($v2['childs'] as $v3) {
                            if (!empty($v3['url'])) {
                                $pn[$v3['id']] = strtolower($v3['url']);
                            }
                        }
                    }
                }
            }
        }
        self::$permission=$pn;
        $key = array_search($url, $pn);
        if ($key) {
            if (empty($user['act_permission'])) {
                returnJson(2001, '无访问权限,请联系管理员开通!');
            } else {
                $ap = explode(',', $user['act_permission']);
                if (in_array($key, $ap)) {
                    return true;
                } else {
                    returnJson(2001, '无访问权限,请联系管理员开通!');
                }
            }
        }
    }
}
